package com.app.twth.app;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.StrictMode;
import android.util.Log;

import com.app.twth.service.PrinterService;
import com.app.twth.utils.CommonUtil;
import com.app.twth.utils.Density;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;


/**
 * ****************************************************************
 * 文件名称: AppApplication
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class MyApplication extends Application {
    private String TAG = getClass().getSimpleName();
    public static String token;
    public static String location;
    public static String versionCode;
    private static MyApplication appInstance;
    public static String PATH_IMG;

    public static PrinterService printerService;

    public static MyApplication getAppInstance() {
        return appInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appInstance = this;
        AppManager.init(this);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            Log.e(TAG, "适用于网络请求数据量很小:");
        }
        //初始化工具类
        CommonUtil.init(this);
        //屏幕适配方案，根据ui图修改,屏幕最小宽度375dp
        Density.setDensity(this, 375f);

        createFilePath();

        /* Bugly SDK初始化
         * 参数1：上下文对象
         * 参数2：APPID，平台注册时得到,注意替换成你的appId
         * 参数3：是否开启调试模式，调试模式下会输出'CrashReport'tag的日志
         */
        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(this);
        try {
            // 设置设备id
            userStrategy.setDeviceID(Build.MANUFACTURER);
            // 设置设备型号
            userStrategy.setDeviceModel(Build.DISPLAY);
        } catch (Throwable thr) {
            CrashReport.postCatchedException(thr);  // bugly会将这个throwable上报
        }
        CrashReport.initCrashReport(this, "fe2445d1da", false, userStrategy);

        setPrinterService();

    }


    public void createFilePath() {
        try {
            //上传图片存放路径
            PATH_IMG = getExternalCacheDir().getPath() + File.separator + "img";
            File file = new File(PATH_IMG);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        LogCatHelper.getInstance(this, PATH_LOG).start();
    }

    public void setPrinterService() {
        //绑定目标Service
        Intent intent = new Intent(this, PrinterService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    // 在Activity中，我们通过ServiceConnection接口来取得建立连接与连接意外丢失的回调
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 建立连接
            // 获取服务的操作对象
            PrinterService.MyBinder binder = (PrinterService.MyBinder) service;
            printerService = binder.getService();// 获取到的Service即MyService
            Log.e(TAG, "onServiceConnected()");
//            printerService.firstConnect();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 连接断开
            Log.e(TAG, "onServiceDisconnected()");
        }
    };
}
